﻿using System.Collections.Generic;
using System.Linq;
using TimeMachine.Engine.Metrics.Impl;
using TimeMachine.Engine.Vcs;

namespace TimeMachine.Engine.Reports
{
	public class WorstCodeStatsCalculator
	{
		private readonly IFilesFilter filesFilter;
		private readonly Dictionary<string, FileStat> stats = new Dictionary<string, FileStat>();
		private string currentFile;

		public WorstCodeStatsCalculator(IFilesFilter filesFilter)
		{
			this.filesFilter = filesFilter;
		}

		public bool OnNextFile(string filename, DetailedCommitInfo commit)
		{
			if(!filesFilter.IsAnalysableSourceFile(filename, commit.Changes)) return false;
			currentFile = filename;
			stats.GetOrCreate(currentFile, () => new FileStat(currentFile, commit.Date)).AddChange();
			return true;
		}

		public void OnNextChange(LineChangeInfo change)
		{
//			stats[currentFile].AddChange();
		}

		public IEnumerable<FileStat> GetStats(int maxCount)
		{
			return stats.Values.OrderBy(stat => -stat.Changes).Take(100);
		}
	}
}